package com.yfree.listeners;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import androidx.annotation.NonNull;

import com.yfree.R;
import com.yfree.activities.YActivity;
import com.yfree.activities.YActivityManager;
import com.yfree.libs.permission.PermissionListener;

import java.util.List;

public abstract class YPermissionListener implements PermissionListener {
    private YActivity context;
    public static int defaultRequestCode = 0;

    protected YPermissionListener(YActivity context) {
        this.context = context;
    }

    @Override
    public final void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
        if (requestCode != defaultRequestCode)
            context.yPermissionCallBacks.remove(requestCode);
        onSuccessed();
    }

    public abstract void onSuccessed();

    @Override
    public void onFailed(final int requestCode, @NonNull final List<String> deniedPermissions) {
        context.yShowBuilder(context.yGetString(R.string.permission_message_permission_failed), "去授权", "完全退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                context.context.startActivityForResult(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).setData(Uri.parse("package:" + context.context.getPackageName())), requestCode);
                YActivityManager.finishApplication();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                YActivityManager.finishApplication();
            }
        }, false);
    }
}
